package com.iterator.util;


import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;

import java.io.File;
import java.io.FileInputStream;
import java.security.MessageDigest;
import java.util.Date;

/**
 * @author: Silence_W
 * @date: 2021-01-14 20:09
 **/
public class IteratorEncrypt {

    public static String getEncryptPassword(String content){

        String temp=content+"iterator";
        String md5 =null;
        try {
           md5 = Md5Util.getMd5(temp);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return md5;

    }




    static  class Md5Util {

        private static String getMd5(String content) throws Exception {
            //获得消息消息摘要算法
            MessageDigest messageDigest = MessageDigest.getInstance("md5");
            byte[] contentBytes = content.getBytes();
            byte[] resultBytes = messageDigest.digest(contentBytes);//byte 8 2^8 ff
            int length = resultBytes.length;
            System.out.println("length:" + length);
            StringBuffer stringBuffer = new StringBuffer();
            for (byte resultByte : resultBytes) {
                int temp = resultByte&0xff; //0-255
                String s = Integer.toHexString(temp); //0-15 0a  05 0-f
                if (s.length() == 1){
                    stringBuffer.append(0);
                }
                stringBuffer.append(s);
            }
            return stringBuffer.toString();


        }
        private static String getMd5(String content,String salt) throws Exception {
            content = content + "&"  + salt + "}";
            return getMd5(content);
        }

        private static String getMiltyMd5(String content) throws Exception {
            return getMd5(getMd5(getMd5(content)),"123456");
        }

        private static String getFileMd5(File file) throws Exception{
            MessageDigest messageDigest = MessageDigest.getInstance("md5");

            FileInputStream fileInputStream = new FileInputStream(file);
            byte[] bytes = new byte[1024];
            int length = 0;
            while ((length = fileInputStream.read(bytes,0,1024)) != -1){
                messageDigest.update(bytes,0,bytes.length);
            }
            byte[] resultBytes = messageDigest.digest();
            StringBuffer stringBuffer = new StringBuffer();
            for (byte resultByte : resultBytes) {
                int temp = resultByte&0xff; //0-255
                String s = Integer.toHexString(temp); //0-15 0a  05 0-f
                if (s.length() == 1){
                    stringBuffer.append(0);
                }
                stringBuffer.append(s);
            }
            return stringBuffer.toString();
        }
    }

}
